package com.huixue.server.rule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.huixue.server.basic.Common;
import com.huixue.server.interfaces.IRule;
import com.huixue.server.mapper.AssignMapper;
import com.huixue.server.model.Assign;

/*!
 * 接口1116
 * 
 * 用于创建分配
 * 
 * 请求：{}
 * 响应：
 * 	[正确]{"code":"200","message":"分配成功"}
 * 	[错误]{"code":"201","message":"分配失败"}
 * 
 * @author look
 *
 */
public class Rule1117 implements IRule {

	public String doIt(SqlSession session, String requestData) {
		AssignMapper assignMapper = session.getMapper(AssignMapper.class);
		List<Assign> assignList = new ArrayList<Assign>();
		try {
			JSONObject jo = new JSONObject(requestData);
			JSONArray assignArray = jo.getJSONArray("list");
			JSONObject assignJson = null;
			Map<String, List<Assign>> assignMap = new HashMap<String, List<Assign>>();
			double begin = System.currentTimeMillis();
			for (int i = 0; i < assignArray.length(); i++) {
				Assign assign = new Assign();
				assignJson = assignArray.getJSONObject(i);
				assign.setBorrowNumber(assignJson.getString("borrow_number"));
				assign.setBorrowName(assignJson.getString("borrow_name"));
				assign.setStoreNumber(assignJson.getString("store_number"));
				assign.setStoreName(assignJson.getString("store_name"));
				assign.setAssignLimit(assignJson.getString("assign_limit"));
				assign.setAssignExpires(assignJson.getString("assign_expires"));
				assign.setOnsetTime(assignJson.getString("onset_time"));
				assign.setCreateTime(Common.getSystemDate().toString());
				assignList.add(assign);
			}
			assignMapper.createAssign(assignList);
			double time = (System.currentTimeMillis() - begin) / 1000;
			System.out.println("插入共花费时间" + time + "s");
			return "{\"code\":\"200\",\"message\":\"分配成功\"}";
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return "{\"code\":\"201\",\"message\":\"分配失败\"}";
	}
}
